<?php
class Catenews_Model {
    public $con;
    public function __construct(){
        $this->con =  mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
        if(mysqli_connect_error())
        {
            echo "Failed to connect to MySQL". mysqli_connect_error();exit();
        }
    }
    public function getListCate(){
        $query = "SELECT c.id,c.nameCat,c.description FROM catnews AS c";
        $result = mysqli_query($this->con,$query);
        return $result;
    }
    public function addCat($nameCat,$description){
        $query = "INSERT INTO catnews (nameCat,description) VALUE ('$nameCat','$description')";
        $result = mysqli_query($this->con,$query);
    }
    public  function delCat($id){
        $query ="DELETE FROM catnews where id='$id'";
        $result = mysqli_query($this->con,$query);
    }
    public function beforeditCat($id){
        $query = "SELECT * FROM catnews WHERE id='$id'";
        $result = mysqli_query($this->con,$query);
        return $result;
    }
    public  function  editCat($nameCat,$description,$id){
        $query ="UPDATE catnews SET namecat='$nameCat' ,description='$description' where  id='$id'";
        $result = mysqli_query($this->con,$query);
    }
    public function viewCat($id){
        $query="SELECT * from catnews where  id='$id'";
        $result = mysqli_query($this->con,$query);
        return $result;
    }
    public function getListProductforEachcat($id){
        $query="SELECT p.id, p.title, p.image, p.content,p.description
		FROM news AS p, catnews AS cp, news_middle AS pc
		where p.id=pc.idproduct
		AND cp.id=pc.idcat
		and cp.id='$id'";
        $result = mysqli_query($this->con,$query);
        return $result;
    }
}
?>